package org.james.deleter.utils;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.james.deleter.components.InfoListBox;

import application.Main;
import javafx.application.Platform;

public class FileOperater {
	
	public static void deleteFilesByKeywords(String folderPath,String keywords,boolean isRecur){
		File folder = new File(folderPath);
		deleteFilesByKeywords(folder, keywords, isRecur);
		InfoListBox.addMessageInfo("执行完毕");
		Platform.runLater(()-> {
			Main.execBtn.setText("确定");
			Main.execBtn.setDisable(false);
		});
		
	}

	public static void deleteFileByExtensions(String folderPath,String extensions,boolean isRecur){
		File folder = new File(folderPath);
		extensions = StringUtils.replace(extensions, " ", "");
		String[] extensionArr = StringUtils.split(extensions,"|");
		
		if(extensionArr.length > 0){
			List<String> extensionList = Arrays.stream(extensionArr).map(e -> e.substring(1))
					   .collect(Collectors.toList());
			deleteFilesByExtensions(folder,extensionList,isRecur);
		}
		InfoListBox.addMessageInfo("执行完毕");
		Platform.runLater(()-> {
			Main.execBtn.setText("确定");
			Main.execBtn.setDisable(false);
		});
		
	}
	
	public static void deleteFilesByExtensions(File file,List<String> extensionList,boolean isRecur){
		if(isRecur){
			deleteFilesRecursive(file, extensionList);
		}else{
			if(file.isDirectory()){
				File[] files = file.listFiles();
				for(File f:files){
					if(f.isDirectory()){
						continue;
					}
					deleteFileByExtensions(f,extensionList);
				}
			}else{
				deleteFileByExtensions(file,extensionList);
			}
		}
	}
	
	public static void deleteFilesRecursive(File file,List<String> extensionList){
		if(file.isDirectory()){
			File[] files = file.listFiles();
			Arrays.stream(files).parallel().forEach((f) -> {
				deleteFilesRecursive(f,extensionList);
			});
		}else{
			deleteFileByExtensions(file,extensionList);
		}
		
	}
	
	public static void deleteFilesRecursiveByKeywords(File file,String keywords){
		if(file.isDirectory()){
			File[] files = file.listFiles();
			Arrays.stream(files).parallel().forEach((f) -> {
				deleteFilesRecursiveByKeywords(f,keywords);
			});
		}else{
			deleteFileByKeywords(file,keywords);
		}
		
	}
	
	public static void deleteFileByExtensions(File file,List<String> extensionList){
		if(file.isDirectory()){
			return;
		}
		String e = FileUtils.getFileExtension(file.getName());
		String path = file.getAbsolutePath();
		if(StringUtils.isNotBlank(e)){
			if(StringUtils.equalAny(e, extensionList.toArray(new String[]{}))){
				file.delete();
				InfoListBox.addMessageInfo("删除文件成功："+path);
			}
		}
	}
	
	public static void deleteFileByKeywords(File file,String keywords){
		if(file.isDirectory()){
			return;
		}
		String name = FileUtils.getNameWithoutExtension(file.getName());
		String path = file.getAbsolutePath();
		if(StringUtils.isNotBlank(name)){
			if(name.contains(keywords)){
				file.delete();
				InfoListBox.addMessageInfo("删除文件成功："+path);
			}
		}
	}
	
	public static void deleteFilesByKeywords(File file,String keywords,boolean isRecur){
		if(isRecur){
			deleteFilesRecursiveByKeywords(file, keywords);
		}else{
			if(file.isDirectory()){
				File[] files = file.listFiles();
				for(File f:files){
					if(f.isDirectory()){
						continue;
					}
					deleteFileByKeywords(file, keywords);
				}
			}else{
				deleteFileByKeywords(file, keywords);
			}
		}
	}

//	public static void deteleFolderOrFile(File file){
//		file.delete();
//	}
}
